package Hibernate.cn.edu.scau.cmi.lishuoxin.dao;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import Hibernate.cn.edu.scau.cmi.lishuoxin.domain.Student;
import Hibernate.cn.edu.scau.cmi.lishuoxin.util.HibernateUtils;

public class StudentDAO {
	private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
	// property constants
	public static final String NAME = "name";

	// 添加学生
	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		Session session = HibernateUtils.openSession();
		Transaction ts = session.beginTransaction();
		try {
			session.save(transientInstance);
			ts.commit();
			log.debug("save successful");
			System.out.println("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	// 删除学生
	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		Session session = HibernateUtils.openSession();
		Transaction ts = session.beginTransaction();
		try {
			session.delete(persistentInstance);
			ts.commit();
			log.debug("delete successful");
			System.out.println("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	// 更新
	public void update(Student student) {
		Session session = HibernateUtils.openSession();
		Transaction ts = session.beginTransaction();
		try {
			session.update(student);
			ts.commit();
			System.out.println("update successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	// 通过id查找学生
	public Student findById(java.lang.Long id) {
		log.debug("getting Student instance with id: " + id);
		Session session = HibernateUtils.openSession();
		try {
			Student instance = (Student) session.get(Student.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	// 通过条件查找学生
	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		Session session = HibernateUtils.openSession();
		try {
			List results = session.createCriteria(Student.class).add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	// 通过属性和值查找学生
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName + ", value: " + value);
		Session session = HibernateUtils.openSession();
		try {
			String queryString = "from Student as model where model." + propertyName + "= ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	// 通过名字查找
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	// 查找所有学生
	public List findAll() {
		log.debug("finding all Student instances");
		Session session = HibernateUtils.openSession();
		try {
			String queryString = "from Student";
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		} finally {
			session.close();
		}
	}

	// 将传入的detached状态的对象的属性复制到持久化对象中，并返回该持久化对象
	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = (Student) HibernateUtils.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	// 将传入的对象持久化并保存。如未保存，save保存；如已保存，update将对象和session重新关联。
	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			HibernateUtils.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	// 将传入的对象状态设置为Transient状态，瞬时状态不和session关联，在数据库中没有关联的记录
	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			HibernateUtils.getCurrentSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}
